package com.gongj.aop.AOPProxy;

import org.aopalliance.aop.Advice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class MyCustomizeAdvisor implements PointcutAdvisor {

	//定义 Pointcut
	@Override
	public Pointcut getPointcut() {
		//方法名称匹配器
		NameMatchMethodPointcut methodPointcut = new NameMatchMethodPointcut();
		methodPointcut.addMethodName("getUserInfo");
		return methodPointcut;
	}

	//定义 Advice
	@Override
	public Advice getAdvice() {
		MethodBeforeAdvice methodBeforeAdvice = new MethodBeforeAdvice() {
			@Override
			public void before(Method method, Object[] args, Object target) throws Throwable {
				System.out.println("方法之前执行");
				//手动执行目标对象的方法
				method.invoke(target, args);
			}
		};
		return methodBeforeAdvice;
	}

	@Override
	public boolean isPerInstance() {
		return false;
	}
}
